<?php
// +----------------------------------------------------------------------
// | title: 标题注释
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2019/7/9 10:39
// +----------------------------------------------------------------------

namespace app\single\controller;


use app\admin\model\ApiOperateModel;
use app\single\model\VarModel;
use cmf\controller\AdminBaseController;

class OperateController extends AdminBaseController
{
    protected $operateModel = null;

    public function initialize()
    {
        parent::initialize();
        $this->operateModel = new ApiOperateModel();
    }

    /**
     * 封号记录
     * @return mixed
     */
    public function blockUserLog()
    {
        //查询时间
        $eDay = $this->request->param("eDay", date("Y-m-d"), "date");
        $sDay = $this->request->param("sDay", date("Y-m-d", strtotime("-7 days")), "date");
        $where[] = ["type", "=", 1];
        $where[] = ["oper_time", "between", [strtotime($sDay . " 00:00:00"), strtotime($eDay . " 23:59:59")]];
        $roleId = $this->request->param("role_id");
        if (!empty($roleId)) {
            $where[] = ["value", "=", $roleId];
        }
        $data = $this->operateModel->alias("a")
            ->where($where)
            ->leftJoin([config("database.game") . ".role" => "b"], "b.id = a.value")
            ->field("a.*,b.name,b.level")
            ->order("oper_time DESC")
            ->paginate($this->request->param("pagesize", 20));
        $data->appends($this->request->param());
        $this->assign("data", $data);
        $this->assign("page", $data->render());
        $this->assign([
            "sDay" => $sDay,
            "eDay" => $eDay
        ]);
        return $this->fetch();
    }

    /**
     * 封禁IP记录
     * @return mixed
     */
    public function blockIpLog()
    {
        //查询时间
        $eDay = $this->request->param("eDay", date("Y-m-d"), "date");
        $sDay = $this->request->param("sDay", date("Y-m-d", strtotime("-7 days")), "date");
        $where[] = ["type", "=", 3];
        $where[] = ["oper_time", "between", [strtotime($sDay . " 00:00:00"), strtotime($eDay . " 23:59:59")]];
        $ip = $this->request->param("ip");
        if (!empty($ip)) {
            $where[] = ["value", "=", $ip];
        }
        $data = $this->operateModel
            ->where($where)
            ->order("oper_time DESC")
            ->paginate($this->request->param("pagesize", 20));
        $data->appends($this->request->param());
        $this->assign("data", $data);
        $this->assign("page", $data->render());
        $this->assign([
            "sDay" => $sDay,
            "eDay" => $eDay
        ]);
        return $this->fetch();
    }

    public function addPlat()
    {
        echo $this->fetch();
    }

    /**
     * 添加平台号
     */
    public function addPlatPost()
    {
        $platform = $this->request->param("platform");
        if ($platform) {
            $model = new VarModel();
            $platforms = $model->getValue("platform_lists");
            $matches = [];
            if (!empty($platforms)) {
                preg_match_all('/<<.*?>>/is', $platforms, $matches);
                $matches[0][] = string2ascii($platform);
                $value = sprintf("[%s]", implode(",", $matches[0]));
                $res = $model->saveData("platform_lists", $value);
            } else {
                $matches[0][] = string2ascii($platform);
                $value = sprintf("[%s]", implode(",", $matches[0]));
                $res = $model->addData("platform_lists", $value);
            }

            if ($res) {
                $this->success("添加成功");
            }
            $this->error("添加失败");
        } else {
            $this->error("请先输入平台号");
        }
    }
}